//
//  ListViewController.m
//  MyCloud
//
//  Created by 张诚 on 7/11/13.
//  Copyright (c) 2013 visitor.zc@gmail.com. All rights reserved.
//

#import "ListViewController.h"
//#import "NetAccess.h"
#import "DeviceViewController.h"
#import "HDRCloud.h"
#import "ListViewCell.h"
#import "ListViewModel.h"
#import "DDSearchAll.h"
#import "DDVerifyThread.h"

@interface ListViewController ()

@property (nonatomic, retain)NSArray *dataArray;

@end

@implementation ListViewController
{
    UITableView *_listView;
    NSMutableArray *_dataArray;
}

@synthesize dataArray;

- (void)dealloc
{
    [_dataArray release];
    [_listView release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    NSArray *devices = [NSArray arrayWithObjects:@"iPhone",@"iPhone", @"iPhone", @"iPhone", @"iPhone", nil];
    NSArray *iPhones = [NSArray arrayWithObjects:@"202.102.1.2", @"202.102.1.2", @"202.102.1.2", @"202.102.1.2", @"202.102.1.2", nil];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:20];
    for (int i = 0 ; i < [devices count]; i++) {
        ListViewModel *model = [[ListViewModel alloc] init];
        model.IPAddress = [iPhones objectAtIndex:i];
        model.deviceName = [devices objectAtIndex:i];
        [array addObject:model];
        [model release];
    }
    self.dataArray = [NSArray arrayWithArray:array];
    
//    NetAccess *net = [NetAccess sharedNetAccess];
//    [net getDeviceList];
    //[net testTCP];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wifi.png"]];
    imageView.frame = CGRectMake(220/2, 80, 100, 100);
    [self.view addSubview:imageView];
    
    
    
    [self.navigationController setNavigationBarHidden:YES];
    self.view.backgroundColor = [UIColor greenColor];
    
    _listView = [[UITableView alloc] init];
    _listView.frame = CGRectMake(0, 200, 320, self.view.frame.size.height - 200);
    _listView.dataSource = self;
    _listView.delegate = self;
    [self.view addSubview:_listView];
    
//    NSData *data = [[NSData alloc] init];
    
    char buffer[16];
    
//    sprintf(buffer,"%s%c%c%c%c","cPxS",0x00010000,0x00010000,0,20);
    NSString *test = [NSString stringWithUTF8String:buffer];
    
//    NSData *testData = [test dataUsingEncoding:NSUTF8StringEncoding];
    
    
    
   /* {"cPxS"};
    a[4] = (char)0x00010000;
    a[5] = (char)0x00010000;
    a[6] = 0;
    a[7] = (char)20;
    a[8] = 0;
    */
//    int x = CRCTest(buffer, 9);
//    NSLog(@"x%d", x);
//    
//    NSString *signture = @"cPxS";
//    NSString *version = @"0x00010000";
//    NSString *cmd = @"0x00000001";
//    int crc = CRCTest(buffer, 9);
//    NSString *crcString = [NSString stringWithFormat:@"%d", crc];
//    int length = 20;
//    NSString *lengthString = [NSString stringWithFormat:@"%d", length];
//    NSArray *array = [NSArray arrayWithObjects: version, cmd, crcString, lengthString, nil];
//    NSData *intData = [self intToData:array];
//    NSData *signtureData = [signture dataUsingEncoding:NSUTF8StringEncoding];
//    NSMutableData *mutableDate = [NSMutableData dataWithData:signtureData];
//    [mutableDate appendData:intData];
//    NSLog(@"mutable --- %@", mutableDate);
//    HDRCloud *hdr = [[HDRCloud alloc] init];
//    hdr.length = 20;
//    hdr.cmd = @"0x00000001";
//    NSData *tmpData = hdr.HDRData;
//    NSLog(@"tmpData --------- %@",tmpData);
    DDSearchAll *searchAll = [[DDSearchAll alloc] init];
//    NSData *tmpData = searchAll.cloudHDR;
//    NSLog(@"tmpData ----- %@",tmpData);
//    [net sentMyDeviceInfo:tmpData];
   // int

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *cellName = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
//    if(cell == nil)
//    {
//        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
//    }
//    cell.textLabel.text = @"123";
//    return cell;
    static NSString *cellName = @"cell";
    ListViewCell *viewCell = (ListViewCell *)[tableView dequeueReusableCellWithIdentifier:cellName];
    if (viewCell == nil) {
        viewCell = [[[ListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName] autorelease];
    }
    viewCell.model = [self.dataArray objectAtIndex:indexPath.row];
    return viewCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请输入密码"
                                                   message:@" "
                                                  delegate:self
                                         cancelButtonTitle:@"取消"
                                         otherButtonTitles:@"确定",nil];
    UITextField *passwordTextField = [[UITextField alloc]init];
    passwordTextField.frame = CGRectMake(50, 40, 200, 30);
    passwordTextField.borderStyle = UITextBorderStyleLine;
    passwordTextField.secureTextEntry = YES;
    passwordTextField.backgroundColor = [UIColor whiteColor];
    [alert addSubview:passwordTextField];
    passwordTextField.tag = 99;
    [passwordTextField becomeFirstResponder];
    
    [alert show];
    [alert release];
    
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    for (UIView *v in alertView.subviews)
    {
        if(v.tag==99)
        {
            // 得到文本框输入的密码
            NSLog(@"%@",((UITextField*)v).text);
            DeviceViewController *dvc = [[DeviceViewController alloc] init];
            [self.navigationController pushViewController:dvc animated:YES];
            [dvc release];
        }
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
